package view;

import model.information.*;
import controller.*;
import java.util.ArrayList;
import java.util.Calendar;

/**
 *
 * @author tobias
 */
public class StartPanel extends javax.swing.JPanel {
    private Controller ctrl;

    /**
     * Creates a new StartPanel (GUI Welcome Screen)
     * @param ctrl the Controller.
     */
    public StartPanel(Controller ctrl) {
        initComponents();
        this.ctrl = ctrl;
        welcomeLabel.setText("Velkommen " + ctrl.getUsr().getUserName());
        Calendar cTime = Calendar.getInstance();
        
        String day;
        switch(cTime.get(Calendar.DAY_OF_WEEK)) {
            case Calendar.MONDAY: day = "Mandag"; break;
            case Calendar.TUESDAY: day = "Tirsdag"; break;
            case Calendar.WEDNESDAY: day = "Onsdag"; break;
            case Calendar.THURSDAY: day = "Torsdag"; break;
            case Calendar.FRIDAY: day = "Fredag"; break;
            case Calendar.SATURDAY: day = "Lørdag"; break;
            case Calendar.SUNDAY: day = "Søndag"; break;
            default: day = "";
        }

        String month;
        switch(cTime.get(Calendar.MONTH)) {
            case Calendar.JANUARY: month = "januar"; break;
            case Calendar.FEBRUARY: month = "februar"; break;
            case Calendar.MARCH: month = "marts"; break;
            case Calendar.APRIL: month = "april"; break;
            case Calendar.MAY: month = "maj"; break;
            case Calendar.JUNE: month = "juni"; break;
            case Calendar.JULY: month = "juli"; break;
            case Calendar.AUGUST: month = "august"; break;
            case Calendar.SEPTEMBER: month = "september"; break;
            case Calendar.OCTOBER: month = "oktober"; break;
            case Calendar.NOVEMBER: month = "november"; break;
            case Calendar.DECEMBER: month = "december"; break;
            default: month = "";
        }

        String timeString = day + " d. " + cTime.get(Calendar.DATE) + ". " + month + " " + cTime.get(Calendar.YEAR);
        dateLabel.setText(timeString);
        updateStartEvents();
    }

    public void updateStartEvents() {
        Calendar nowDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        endDate.roll(Calendar.MONTH, true);

        ArrayList<Event> commingEvents = new ArrayList<Event>();
        for(Event e: ctrl.getEvents()) {
            if(e.getStartTime().after(nowDate) && e.getEndTime().before(endDate)) {
                commingEvents.add(e);
            }
        }
        eventList.setListData(commingEvents.toArray());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        welcomeLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        eventList = new javax.swing.JList();
        dateLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(816, 586));

        welcomeLabel.setFont(welcomeLabel.getFont().deriveFont(welcomeLabel.getFont().getStyle() | java.awt.Font.BOLD, welcomeLabel.getFont().getSize()+3));
        welcomeLabel.setText("Velkommen");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Kommende aftaler:"));
        jPanel1.setOpaque(false);
        jPanel1.setPreferredSize(new java.awt.Dimension(230, 518));

        eventList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(eventList);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
                .addContainerGap())
        );

        dateLabel.setFont(dateLabel.getFont().deriveFont(dateLabel.getFont().getStyle() | java.awt.Font.BOLD, dateLabel.getFont().getSize()+3));
        dateLabel.setText("Dato");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/lifeManagerStor.png"))); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(welcomeLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 641, Short.MAX_VALUE)
                        .add(dateLabel)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 81, Short.MAX_VALUE)
                        .add(jLabel1)
                        .add(73, 73, 73))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(welcomeLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .add(dateLabel))
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(70, 70, 70))))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel dateLabel;
    private javax.swing.JList eventList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel welcomeLabel;
    // End of variables declaration//GEN-END:variables

}
